$('document').ready(function() {
	if ($('.ctspdangdau').length > 0)
		setInterval(function() {
			getProductDetails(auctionList);
		}, 500);
});

function getProductDetails(auctionList) {

	var list = auctionList.join(",");
	$.post('../ajax/getAuction.php', {
		auctions : list
	}, function(data) {
		data = $.parseJSON(data);
		$
				.each(data,
						function(key, value) {
							var auction = $('[ref="bid-' + value.auctionID
									+ '"] .ctspdangdau');

							auction.children('.divTimeLeft').html(
									sec2Time(value.timeleft));
							auction.find('.gia > span').html(
									parseInt(value.auction_price) + "đ");
							if (value.current_user_bid === null) {
								auction.find('.nguoidau > span').html(
										"--------------");
							} else
								auction.find('.nguoidau >span').html(
										value.current_user_bid);

						})
	});
};

function bid(auctionID) {
	$.getJSON('/ajax/bidProduct.php', {
		'auctionID' : auctionID
	}, function(data) {
		if (data.success !== true) {
			$('[ref="bid-' + auctionID + '"] .divErrors' + auctionID).html(
					'có lỗi').show();
		} else {
			$('[ref="bid-' + auctionID + '"] .divErrors' + auctionID).hide();
			$('[ref="bid-' + auctionID + '"] nguoidau span')
					.html(data.username);
		}
	});

}

function sec2Time(secs) {
	var hours = Math.floor(secs / (60 * 60));

	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);

	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);

	return hours.pad() + ":" + minutes.pad() + ":" + seconds.pad();
}

Number.prototype.pad = function(size) {
	if (typeof (size) !== "number") {
		size = 2;
	}
	var s = String(this);
	while (s.length < size)
		s = "0" + s;
	return s;
};